//
//  YSGMeetingActionView.swift
//  EyeVisionForDoctors
//
//  Created by PMC on 2022/9/19.
//  Copyright © 2022 tao Tao. All rights reserved.
//

import UIKit

class YSGMeetingActionView: UIView {
    public let handup = YSGMeetingActionButton(icon: "xylink_reject_icon", title: "挂断")
    public let audio = YSGMeetingActionButton(icon: "xylink_voice_icon", selectedIcon: "xylink_camera_icon", title: "切换语音模式", selectedTitle: "切换视频模式")
    public let mute = YSGMeetingActionButton(icon: "xylink_unmute_icon", selectedIcon: "xylink_mute_icon", title: "静音", selectedTitle: "取消静音")
    public let camera = YSGMeetingActionButton(icon: "xylink_switchcamera_icon", title: "切换摄像头")
    
    init() {
        super.init(frame: .zero)
        
        addSubview(handup)
        handup.snp.makeConstraints { make in
            make.bottom.centerX.equalToSuperview()
            make.width.equalTo(80)
        }
        
        
        addSubview(mute)
        mute.snp.makeConstraints { make in
            make.top.centerX.equalToSuperview()
            make.bottom.equalTo(handup.snp.top).offset(-20)
            make.width.equalTo(handup)
        }
        
        addSubview(audio)
        audio.snp.makeConstraints { make in
            make.right.equalTo(mute.snp.left).offset(-20)
            make.top.width.equalTo(mute)
            make.left.equalToSuperview()
        }
        
        addSubview(camera)
        camera.snp.makeConstraints { make in
            make.left.equalTo(mute.snp.right).offset(20)
            make.top.width.equalTo(mute)
            make.right.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class YSGMeetingActionButton: UIControl {
    override var isSelected: Bool {
        didSet {
            titleLabel.text = isSelected ? (selectedTitle ?? title) : title
            iconImage.image = isSelected ? UIImage(named: selectedIcon ?? icon) : UIImage(named: icon)
        }
    }
    private let title: String
    private let selectedTitle: String?
    private let icon: String
    private let selectedIcon: String?
    
    private let iconImage = UIImageView()
    private let titleLabel = UILabel()
    init(icon: String, selectedIcon: String? = nil, title: String, selectedTitle: String? = nil) {
        self.title = title
        self.selectedTitle = selectedTitle
        self.icon = icon
        self.selectedIcon = selectedIcon
        super.init(frame: .zero)
        
        addSubview(iconImage)
        iconImage.snp.makeConstraints { make in
            make.top.centerX.equalToSuperview()
            make.width.equalTo(60)
            make.height.equalTo(60)
        }
        iconImage.image = UIImage(named: icon)
        
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.bottom.equalToSuperview()
            make.top.equalTo(iconImage.snp.bottom).offset(5)
        }
        titleLabel.font = .regular_14
        titleLabel.textColor = .white
        titleLabel.text = title
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
